package com.khshop.controller;


import com.khshop.pojo.Admin;
import com.khshop.service.AdminService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.RegisterForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-08
 */
@Api(tags = "管理员 前端控制器")
@Controller
@RequestMapping("/")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("跳转首页")
    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @ApiOperation("跳转登录页")
    @GetMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @ApiOperation("跳转demoList页")
    @GetMapping("/menuList")
    public String menuList(){
        return "menuList";
    }

    @ApiOperation("跳转注册页")
    @GetMapping("/toRegister")
    public String toRegister(){
        return "login";
    }

    @ApiOperation("登录方法，返回登录结果")
    @PostMapping("/adminLogin")
    @ResponseBody
    public AjaxResult login(@RequestBody Admin loginAdmin, HttpSession session){
        
        System.err.println("loginAdmin:"+loginAdmin);
        Admin resultAdmin = adminService.getAdmin(loginAdmin.getUname());
        System.err.println("resultAdmin:"+resultAdmin);

        if (resultAdmin==null){
            System.out.println("账号不存在！");
            return AjaxResult.error("账号不存在！");
        }
        if (!resultAdmin.getInuse()){
            System.out.println("账号已被停用！");
            return AjaxResult.error("账号已被停用！");
        }
        //判断密码对错
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean flag = bcryptPasswordEncoder.matches(loginAdmin.getUpass(),resultAdmin.getUpass());
        System.err.println("flag:"+flag);
        if (!flag){
            System.out.println("密码错误！");
            return AjaxResult.error("密码错误！");
        }
        session.setAttribute("USER_LOGIN",resultAdmin);
        System.out.println("登录成功！");
        return AjaxResult.success("登录成功！");
    }

    @ApiOperation("注册方法，返回注册结果")
    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(@RequestBody @ApiParam("注册用VO")RegisterForm registerForm){

        System.err.println("注册表单信息:"+registerForm);
        // 表单密码重复判断
        if (!registerForm.getUpass().equals(registerForm.getCheckPwd())){
            return AjaxResult.error("两次密码输入不一致");
        }
        // 用户名已存在
        Admin resultAdmin = adminService.getAdmin(registerForm.getUname());
        if (resultAdmin!=null){
            return AjaxResult.error("用户名已存在");
        }
        // 构建用户对象
        Admin admin=new Admin();
        admin.setUname(registerForm.getUname());
        // 密码加密
        String registerUpass=registerForm.getUpass();
        String bCryptPassword = new BCryptPasswordEncoder().encode(registerUpass);
        admin.setUpass(bCryptPassword);
        admin.setTele(registerForm.getTele());
        admin.setWechat(registerForm.getWechat());
        admin.setRoleid(registerForm.getRoleid());
        System.err.println(admin);
        // 保存对象！
        if (adminService.addAdmin(admin)){
            return AjaxResult.success("新用户注册成功");
        }else {
            return AjaxResult.error("注册失败，请稍后重试！");
        }
    }

    @ApiOperation("更新方法，返回更新结果")
    @PostMapping("/updateAdmin")
    @ResponseBody
    public AjaxResult updateAdmin(@RequestBody Admin admin,@RequestParam("checkPwd") String checkPwd){
        // 表单密码重复判断
        if (!admin.getUpass().equals(checkPwd)){
            return AjaxResult.error("两次密码输入不一致");
        }
        // 用户名已存在
        Admin resultAdmin = adminService.getAdmin(admin.getUname());
        if (resultAdmin!=null){
            return AjaxResult.error("用户名已存在");
        }

        // 密码加密
        String bCryptPassword = new BCryptPasswordEncoder().encode(admin.getUpass());
        admin.setUpass(bCryptPassword);
        if (adminService.updateAdmin(admin)){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败，请稍后重试！");
        }
    }

    @ApiOperation("逻辑删除方法，返回删除结果")
    @PostMapping("/delAdmin")
    @ResponseBody
    public AjaxResult delAdmin(Long id){
        
        if (adminService.delAdmin(id)){
            return  AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败，请稍后重试！");
        }
    }

    @ApiOperation("退出登录方法，销毁session")
    @GetMapping("/logout")
    @ResponseBody
    public String logout(HttpSession  session){
        session.invalidate();
        return "login";
    }

//    @ApiOperation("页面自动跳转")
//    @GetMapping("/{url}")
//    public String redirect(@PathVariable("url") String url){
//        return url;
//    }

}

